const registerBtn = document.querySelector('#btn-register')
registerBtn.addEventListener('click', async function (e) {
    e.preventDefault()
    const form = document.querySelector('form')
    const formData = serialize(form, { hash: true, empty: true })
    console.log(formData.username);
    console.log(formData.password);
    // 非空校验
    if (formData.username.trim().length === 0 || formData.password.trim().length === 0) {
        return showTost('账号密码不能为空')
    }
    // 长度校验
    if (formData.username.trim().length < 8 || formData.password.trim().length < 6 ||
        formData.username.trim().length > 30 || formData.password.trim().length > 30) {
        return showTost('账号长度为8-30，密码长度为8-30')
    }
    // 发起请求
    try {
        const res = await axios.post('register', { ...formData })
        showTost(res.data.message)
        setTimeout(() => {
            location.href = './login.html'
        }, 1000)
    } catch (err) {
        showTost('账户被占用')
    }

})